package com.spc.car.data.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spc.car.data.entity.sys.SysUserRole;
import com.spc.car.data.mapper.sys.SysUserRoleMapper;
import com.spc.car.data.service.sys.SysUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户和角色关联表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-03-06
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    /**
     * 通过角色ID查询角色使用数量
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Override
    public int countUserByRoleId(Long roleId) {
        return count(new QueryWrapper<SysUserRole>().eq("role_id", roleId));
    }

    @Override
    public boolean deleteByUserId(Long userId) {
        return remove(new QueryWrapper<SysUserRole>().eq("user_id", userId));
    }

    @Override
    public Boolean CheckAdminByUserId(Long userId) {
        int oneAdmin = count(new QueryWrapper<SysUserRole>()
                .eq("user_id", userId)
                .eq("role_id", 1)
        );
        if (oneAdmin == 1){
            return true;
        }
        return false;
    }

}
